package com.primeton.wh.mcenter.impl.message.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.primeton.wh.mcenter.api.ApplicationClient;
import com.primeton.wh.mcenter.api.model.Application;
import com.primeton.wh.mcenter.api.response.Result;
import com.primeton.wh.mcenter.api.response.ResultGenerator;
import com.primeton.wh.mcenter.impl.message.service.IApplicationService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2020-03-19 16:47
 * @description:
 **/
@RestController
public class ApplicationController implements ApplicationClient{

    @Autowired
    private IApplicationService applicationService;


    /**
     * 查询应用
     *
     * @param page    :
     * @param limit   :
     * @param appName :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */
    public Result list( long page,
                        long limit,
                        String msgTo,
                        String appName) {

        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("msgTo", msgTo);
        condition.put("appName", appName);

        IPage<Application> applicationIPage = null;
        try {
            applicationIPage = applicationService.getApplicationList(condition);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(applicationIPage);
    }



    /**
     * Application新增
     *
     * @param record :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */

    public Result add( Application record) {
        try {
            Map<String, String> result = applicationService.addApplication(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * Application修改
     *
     * @param appId  :
     * @param record :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */

    public Result update( String appId,
                          Application record) {
        try {
            Map<String, String> result = applicationService.editApplication(record);
            return ResultGenerator.genOkResult();
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

}
